package com.czl.module_main.adapter

import android.Manifest
import android.annotation.SuppressLint
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.core.app.ActivityCompat
import androidx.core.content.ContentProviderCompat.requireContext
import com.blankj.utilcode.util.PhoneUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.czl.base.binding.command.BindingCommand
import com.czl.base.binding.command.BindingConsumer
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.CommunityResponseBean
import com.czl.base.data.bean.HouseKeeperListBean
import com.czl.base.util.PermissionUtil
import com.czl.module_main.R
import com.czl.module_main.databinding.ItemHousekeepersBinding
import com.czl.module_main.fragment.HouseKeepersFragment

class HouseKeeperAdapter (private val fragment: HouseKeepersFragment) :
    BaseQuickAdapter<HouseKeeperListBean.Record, BaseDataBindingHolder<ItemHousekeepersBinding>>(
        R.layout.item_housekeepers
    ){
    override fun convert(
        holder: BaseDataBindingHolder<ItemHousekeepersBinding>,
        item: HouseKeeperListBean.Record
    ) {
        holder.dataBinding?.apply {
            data = item
            adapter = this@HouseKeeperAdapter
            executePendingBindings()
        }
    }
    @SuppressLint("MissingPermission")
    val onPhoneClick: BindingCommand<Any> = BindingCommand(BindingConsumer {
        if (it is HouseKeeperListBean.Record){
            PermissionUtil.reqCall(null, fragment) { allGranted, _, _ ->
                if (allGranted) {
                    if (ActivityCompat.checkSelfPermission(
                            fragment.requireContext(),
                            Manifest.permission.CALL_PHONE
                        ) == PackageManager.PERMISSION_GRANTED
                    ) {
                        PhoneUtils.call(it.phone)
                    }

                }
            }
        }
    })
}